<template>
  <div>
    <FixedOperation>
      <!-- 内容 -->
      <div class="text-right">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="danger" @click="close">关闭</el-button>
      </div>
    </FixedOperation>

    <div class="px-10">
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="130"
        label-position="right"
      >
        <el-collapse v-model="activeNames">
          <el-collapse-item name="info">
            <template #title>
              <title-name title="基本信息" icon="i-carbon-bookmark-filled" />
            </template>
            <el-row :gutter="formGutter" class="p-10">
              <el-col :span="formCol">
                <el-form-item label="姓名：" prop="co.contactName">
                  <el-input
                    v-model="form.co.contactName"
                    placeholder="填写姓名"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="类别：" required prop="co.contactType">
                  <!-- 客户类别 -->
                  <el-select
                    class="w-full"
                    v-model="form.co.contactType"
                    placeholder="类别"
                  >
                    <el-option
                      v-for="(dict, index) in contacts_class"
                      :key="index"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="尊称：">
                  <el-radio-group v-model="form.co.sex">
                    <el-radio
                      v-for="(item, index) in sys_user_sex"
                      :label="item.value"
                      :key="index"
                    >
                      {{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="联系电话：" prop="co.contactNumber">
                  <el-input
                    v-model="form.co.contactNumber"
                    placeholder="填写联系电话"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="身份证号/编号：" prop="co.idNumber">
                  <el-input
                    v-model="form.co.idNumber"
                    placeholder="输入身份证号/编号"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="业务分类" prop="co.businessTypeArr">
                  <el-tree-select
                    style="width: 100%"
                    v-model="form.co.businessTypeArr"
                    :data="userBusinessTypeOption"
                    :multiple="true"
                    :check-strictly="false"
                    :show-checkbox="true"
                    placeholder="请选择业务分类"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="角色：">
                  <el-select
                    v-model="form.co.roleCode"
                    placeholder="请选择角色"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in contacts_role"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="formCol">
                <el-form-item label="职务：">
                  <el-select
                    v-model="form.co.dutyCode"
                    placeholder="请选择职务"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in duties_dict"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="岗位：">
                  <el-select
                    v-model="form.co.postCode"
                    placeholder="请选择岗位"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in post_dict"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="生日：">
                  <el-date-picker
                    style="width: 100%"
                    v-model="form.co.birthday"
                    type="date"
                    placeholder="请选择日期"
                    value-format="YYYY-MM-DD 00:00:00"
                    format="YYYY-MM-DD"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="所属企业：">
                  <el-select
                    v-model="form.co.entId"
                    placeholder="请选择所属企业"
                    class="w-full"
                    filterable
                    remote
                    :remote-method="queryCustomer"
                  >
                    <el-option
                      v-for="item in parentOptions"
                      :key="item.id"
                      :label="item.customerName"
                      :value="item.id"
                    >
                      <div>
                        <span>{{ item.customerName }}</span>
                        <span>({{ item.mainContactName }})</span>
                      </div>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="固定电话：">
                  <el-input
                    v-model="form.co.telephone"
                    placeholder="请输入固定电话"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="电子邮箱：">
                  <el-input
                    v-model="form.eo.email"
                    placeholder="请输入电子邮箱"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="传真号码：">
                  <el-input
                    v-model="form.eo.fax"
                    placeholder="请输入传真号码"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="QQ号码：">
                  <el-input
                    v-model="form.eo.qq"
                    placeholder="请输入QQ号码"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="微信账号：">
                  <el-input
                    v-model="form.eo.wechat"
                    placeholder="请输入微信账号"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item
                  label="地址说明："
                  prop="co.addressName"
                  :rules="[
                    {
                      max: 10,
                      message: '地址说明不能超过10个字符',
                      trigger: 'blur'
                    }
                  ]"
                >
                  <el-input
                    v-model="form.co.addressName"
                    placeholder="请输入地址说明,如：办公、注册、分机"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="详细地址：">
                  <el-input
                    v-model="form.co.address"
                    placeholder="请输入详细地址"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <el-collapse-item name="append">
            <template #title>
              <title-name title="附加信息" icon="i-carbon-bookmark-filled" />
            </template>
            <el-row :gutter="formGutter" class="p-10">
              <el-col :span="formCol">
                <el-form-item label="归属公司：" required>
                  <el-tree-select
                    style="width: 100%"
                    v-model="form.co.orgId"
                    :data="companyOptions"
                    :props="{
                      value: 'id',
                      label: 'label',
                      children: 'children'
                    }"
                    placeholder="请选择归属公司"
                    value-key="id"
                    filterable
                  />
                </el-form-item>
              </el-col>

              <el-col :span="formCol">
                <el-form-item label="销售经理：" required>
                  <el-select
                    v-model="form.co.userId"
                    placeholder="请选择销售经理"
                    @focus="userInputFocus"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in selectOptionObj['userId']"
                      :key="index"
                      :label="item.label"
                      :value="item.uuid"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="行业类型：">
                  <el-select
                    v-model="form.co.industryTypeCode"
                    placeholder="请选择行业类型"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in industry_category"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="单位所在地区：">
                  <el-input
                    v-model="form.eo.unitAddress"
                    clearable
                    placeholder="请输入单位所在地区"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="单位所属行业：">
                  <el-input
                    v-model="form.eo.unitBusiness"
                    clearable
                    placeholder="请输入单位所属行业"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="持有证书：">
                  <el-tree-select
                    v-model="form.eo.hasCertificate"
                    :data="certificateName"
                    :render-after-expand="false"
                    class="w-full"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="年检年度：">
                  <el-date-picker
                    v-model="form.eo.checkYear"
                    type="year"
                    style="width: 100%"
                    format="YYYY"
                    value-format="YYYY"
                    placeholder="请选择年检年度"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="是否采集入库：">
                  <el-select
                    v-model="form.eo.isCollect"
                    placeholder="是否采集入库"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in library_status"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="formCol">
                <el-form-item label="星级：">
                  <el-rate v-model="form.co.starRating" />
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="状态：">
                  <el-select
                    v-model="form.co.status"
                    placeholder="请选择状态"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in customer_status"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="信用等级：">
                  <el-select
                    v-model="form.eo.creditListId"
                    placeholder="请选择信用等级"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in credit_rating"
                      :key="index"
                      :label="item.label"
                      :value="item.value * 1"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="来源：">
                  <el-select
                    v-model="form.co.origin"
                    placeholder="请选择来源"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in customer_source"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="征信积分：">
                  <el-input
                    v-model="form.eo.creditScore"
                    placeholder="请输入征信积分"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="所在城市：">
                  <el-cascader
                    class="w-full"
                    clearable
                    placeholder="请选择地址"
                    style="width: 100%"
                    :options="regionData"
                    v-model="form.co.cityCode"
                  >
                  </el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="备注信息：">
                  <el-input
                    v-model="form.co.remark"
                    placeholder="请输入备注信息"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
          <!--    培训产业      -->
          <el-collapse-item name="training" v-if="form.co.contactType == '2'">
            <template #title>
              <title-name
                title="培训产业联系人特殊信息"
                icon="i-carbon-bookmark-filled"
              />
            </template>
            <el-row :gutter="formGutter" class="p-10">
              <el-col :span="24">
                <el-form-item label="培训产业：" required>
                  <el-radio-group v-model="form.eo.cultivateIndus">
                    <el-radio
                      :label="item.value"
                      v-for="(item, index) in training_industry"
                      :key="index"
                      >{{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="满25周岁：">
                  <el-radio-group v-model="form.eo.isTwentyfive">
                    <el-radio
                      :label="item.value"
                      v-for="(item, index) in sys_yes_no"
                      :key="index"
                      >{{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="层次">
                  <el-select
                    v-model="form.eo.level"
                    placeholder="请选择层次"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in levelsOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="籍贯：">
                  <el-input
                    v-model="form.eo.nativePlace"
                    placeholder="填写籍贯"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="工作单位：">
                  <el-input
                    v-model="form.eo.workPlace"
                    placeholder="填写工作单位"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="民族：">
                  <el-input
                    v-model="form.eo.nation"
                    placeholder="填写民族"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="身份证地址：">
                  <el-input
                    v-model="form.eo.idAddress"
                    placeholder="填写身份证地址"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="政治面貌：">
                  <el-input
                    v-model="form.eo.politicsFace"
                    placeholder="填写政治面貌"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="家庭住址：">
                  <el-input
                    v-model="form.eo.familyAddress"
                    placeholder="填写家庭住址"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="formCol">
                <el-form-item label="最高学历：">
                  <el-select
                    v-model="form.eo.education"
                    placeholder="请选择最高学历"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in degreeOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="学校名称：">
                  <el-input
                    v-model="form.eo.school"
                    placeholder="填写学校名称"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="学制：">
                  <el-select
                    v-model="form.eo.eduSystem"
                    placeholder="请选择学制"
                    class="w-full"
                    clearable
                  >
                    <el-option
                      v-for="(item, index) in educationalOptions"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="有无证件">
                  <el-radio-group v-model="form.eo.isCert">
                    <el-radio label="1">有</el-radio>
                    <el-radio label="0">无</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="学习形式：">
                  <el-input
                    v-model="form.eo.studyForm"
                    placeholder="填写学习形式"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="毕业时间：">
                  <el-date-picker
                    style="width: 100%"
                    v-model="form.eo.graduateTime"
                    type="date"
                    placeholder="请选择日期"
                    value-format="YYYY-MM-DD 00:00:00"
                    format="YYYY-MM-DD"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="毕业证书编号：">
                  <el-input
                    v-model="form.eo.certNum"
                    placeholder="填写毕业证书编号"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="formCol">
                <el-form-item label="所在微信群：">
                  <el-input
                    v-model="form.eo.chatGroup"
                    placeholder="填写所在微信群"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
        </el-collapse>
      </el-form>
      <!--  弹窗选择组件  -->
      <SelectDialog :dialog-param="dialogParam" @ok="selectData"></SelectDialog>
    </div>
  </div>
</template>
<script setup name="ContactsAddOrUpdate">
import { getCurrentInstance } from 'vue'
import SelectDialog from '@/components/SelectDialog/index.vue'
import FixedOperation from '@/components/FixedOperation'
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
import useTagsViewStore from '@/store/modules/tagsView'
import useUserStore from '@/store/modules/user'
import useCommonStore from '@/store/modules/common'
import { validatePhoneTwo, validateIdNo } from '@/utils/validate'
import { getTreeNodeByParam, getUniqueUserId } from '@/utils/commo'
import {
  userBusinessTypeOption //用户业务类型树
} from '@/hooks/dict/order'
import {
  createContact,
  getContactInfo,
  updateContact
} from '@/api/customerManage/contactList'
import {
  getCustomerInfo,
  getCustomerList,
  getListByCustomerName
} from '@/api/customerManage/customerList'

// 通用数据store
const commonStore = useCommonStore()
// 用户数据
const userStore = useUserStore()

const { proxy } = getCurrentInstance()
const router = useRouter()
// 字典 具体查看根目录DICT.md文件
const {
  industry_category,
  contacts_class,
  tax_grade,
  credit_rating,
  post_dict,
  duties_dict,
  customer_status,
  customer_source,
  invoice_type,
  invoice_header,
  sys_user_sex,
  contacts_role,
  training_industry,
  sys_yes_no,
  library_status
} = proxy.useDict(
  'industry_category',
  'contacts_class',
  'tax_grade',
  'credit_rating',
  'post_dict',
  'duties_dict',
  'customer_status',
  'customer_source',
  'invoice_type',
  'invoice_header',
  'sys_user_sex',
  'contacts_role',
  'training_industry',
  'sys_yes_no',
  'library_status'
)

import {
  certificateName // 报考证书名称
} from '@/hooks/dict/order'

const activeNames = ref(['info', 'append', 'contacts', 'bill', 'training'])
const data = reactive({
  form: {
    co: {
      contactType: '1',
      origin: '9'
    },
    eo: {
      cultivateIndus: '1',
      isTwentyfive: 'Y'
    }
  },
  dialogParam: {}, //弹窗组件配置
  selectOptionObj: {}, // select
  rules: {
    // 联系人姓名
    'co.contactName': [
      { required: true, message: '请输入联系人名称', trigger: 'blur' }
    ],
    'co.contactNumber': [
      { required: true, validator: validatePhoneTwo, trigger: 'blur' }
    ],
    'co.businessTypeArr': [
      { required: true, message: '请选择业务分类', trigger: 'change' }
    ],
    'co.idNumber': [
      { required: true, message: '请输入身份证号码/编号', trigger: 'blur' }
    ]
  },
  // 层次
  levelsOptions: [
    { label: '中专', value: '1' },
    { label: '高起专', value: '2' },
    { label: '专升本', value: '3' }
  ],
  // 最高学历
  degreeOptions: [
    { label: '初中及以下', value: '1' },
    { label: '中专或高中', value: '2' },
    { label: '大专', value: '3' },
    { label: '本科', value: '4' }
  ],
  // 学制
  educationalOptions: [
    { label: '全日制', value: '1' },
    { label: '在职', value: '2' }
  ]
})
const {
  form,
  dialogParam,
  selectOptionObj,
  rules,
  levelsOptions,
  degreeOptions,
  educationalOptions
} = toRefs(data)
const formCol = ref(12)
const formGutter = ref(20)

const companyOptions = ref([])
const parentOptions = ref([]) //上级企业数据

/**
 * 获取联系人信息
 */
function getContactsInfoData() {
  let id = router.currentRoute.value.query.contactsId
  getContactInfo(id).then(res => {
    let obj = JSON.parse(JSON.stringify(res.data))
    // 转换城市代码
    if (obj.co.cityCode != null) {
      obj.co.cityCode = obj.co.cityCode.split(',')
    }
    // 转换星级字段属性
    if (obj.co.starRating != null) {
      obj.co.starRating = parseInt(obj.co.starRating)
    }
    //获取所属企业信息
    if (obj.co.entId != null && obj.co.entId != '0') {
      getCustomerInfo(obj.co.entId).then(res => {
        queryCustomer(res.data.vo.customerName)
      })
    } else {
      obj.co.entId = undefined
    }
    if (obj.co.contactType == null) {
      obj.co.contactType = '1'
    }
    getDefaultUserInfo(obj.co.userId)
    form.value = obj
  })
}

/**
 * 根据当前用户回填归属人员表单
 */
async function getDefaultUserInfo(userId) {
  let id = ''
  if (userId) {
    id = userId
  } else {
    //获取当前用户id
    id = userStore.user.userId
  }
  //获取部门人员数据
  const deptUserData = await commonStore.getUseTreeData()

  //根据当前用户id获取uuid
  let obj = getTreeNodeByParam(deptUserData, id, 'id', [])
  selectOptionObj.value.userId = obj
  form.value.co.userId = obj[0].uuid
}

/**
 * 上级企业模糊搜索
 * @param val 当前输入的值
 */
function queryCustomer(val) {
  if (!val) {
    return
  }
  let params = {
    pageSize: 10,
    customer: val
  }
  getListByCustomerName(params).then(res => {
    parentOptions.value = res.data
  })
}

/**
 * 保存方法
 */
function save() {
  proxy.$refs.formRef.validate((valid, fields) => {
    if (valid) {
      console.log(valid)
      let data = JSON.parse(JSON.stringify(toRaw(form.value)))
      if (form.value.co.cityCode) {
        // 转换所在区域城市编码
        data.co.cityCode = form.value.co.cityCode.join(',')
      }
      // 销售经理
      if (form.value.co.userId) {
        // 获取查询到的user数据对象
        let userObj = getUniqueUserId(
          selectOptionObj.value['userId'],
          form.value.co.userId
        )
        //获取userid
        data.co.userId = userObj.id

        //获取deptId
        data.co.deptId = userObj.deptId

        //   获取userName
        data.co.userName = userObj.label
      }
      data.co.entType = '1'
      // 判断联系人是否有所属企业 如果没有则设置为0
      if (!data.co.entId) {
        data.co.entId = '0'
      }
      if (router.currentRoute.value.query.flag == 'add') {
        //   新增联系人
        createContact(data).then(res => {
          proxy.$modal.msgSuccess(res.msg)
        })
      } else {
        //   修改联系人
        updateContact(data).then(res => {
          proxy.$modal.msgSuccess(res.msg)
        })
      }
      console.info('form.value', form.value)
    } else {
      proxy.$nextTick(() => {
        document.getElementsByClassName('is-error')[0].scrollIntoView({
          behavior: 'smooth',
          block: 'center'
        })
      })
    }
  })
}

/**
 * 关闭方法
 */
function close() {
  useTagsViewStore().delView(router.currentRoute.value)
  router.go(-1)
}

/**
 * 弹窗选取获取选中的值
 * @param val
 *  type 业务类型
 *     selectData 选中的数据[{value: 1, label: '王晓易', $treeNodeId: 2}]
 *     labelData 选中的数据label ['王晓易']
 *     defaultCheckedKeys 选中的数据id [1]
 *     dialogVisible: false 弹出框的关闭
 */
function selectData(val) {
  console.info('val', val)
  //归属人员
  if (val.type == 'userId') {
    if (!val.selectData.length) {
      form.value.co[val.type] = ''
    } else {
      selectOptionObj.value[val.type] = val.selectData
      form.value.co[val.type] = val.defaultCheckedKeys[0]
    }
  }
}

/**
 * 归属人员input获取焦点事件
 */
function userInputFocus() {
  dialogParam.value.type = 'userId'
  dialogParam.value.title = '选择销售经理'
  dialogParam.value.dataType = 'person'
  dialogParam.value.multiple = false
  dialogParam.value.checkStrictly = false
  dialogParam.value.nodeKey = 'uuid'

  // selectOptionObj.value['userId'] = []
  if (form.value.co['userId'] && form.value.co['userId'].length) {
    dialogParam.value.isDialog = true
    dialogParam.value.defaultCheckedKeys = [form.value.co['userId']]
  } else {
    dialogParam.value.isDialog = true
    dialogParam.value.defaultCheckedKeys = []
  }
}

/**
 * 初始化
 */
async function init() {
  // 获取归属公司数据
  await commonStore.getCompanyData().then(data => {
    companyOptions.value = data
  })
  if (router.currentRoute.value.query.flag == 'add') {
    if (
      router.currentRoute.value.query.customerId &&
      router.currentRoute.value.query.customerId !== '0'
    ) {
      getCustomerInfo(router.currentRoute.value.query.customerId).then(res => {
        queryCustomer(res.data.vo.customerName)
        form.value.co.entId = router.currentRoute.value.query.customerId
      })
    }
    form.value.co.orgId = userStore.user.orgId

    await getDefaultUserInfo()
  } else {
    // 获取获取联系人信息
    getContactsInfoData()
  }
}

init()
</script>
<style lang="scss" scoped></style>
